package dal

import (
	"database/sql"
	"fmt"
	"strconv"

	"ifashion/admin/model"

	_ "github.com/go-sql-driver/mysql"
)

type rec struct {
}
type RecHotByBM []model.RecHot

func (a RecHotByBM) Len() int {
	return len(a)
}
func (a RecHotByBM) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}
func (a RecHotByBM) Less(i, j int) bool {
	m, _ := strconv.Atoi(a[i].BM)
	n, _ := strconv.Atoi(a[j].BM)
	return m < n
}

//排序函数
// type ClothByBM []model.Cloth

// func (a ClothByBM) Len() int {
// 	return len(a)
// }
// func (a ClothByBM) Swap(i, j int) {
// 	a[i], a[j] = a[j], a[i]
// }
// func (a ClothByBM) Less(i, j int) bool {
// 	m, _ := strconv.Atoi(a[i].ID)
// 	n, _ := strconv.Atoi(a[j].ID)
// 	return m < n
// }

type SRecByBM []model.SRec

func (a SRecByBM) Len() int {
	return len(a)
}
func (a SRecByBM) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}
func (a SRecByBM) Less(i, j int) bool {
	m, _ := strconv.Atoi(a[i].BM)
	n, _ := strconv.Atoi(a[j].BM)
	return m < n
}

func (rec) Lst(num model.Num) (qrs []model.Cloth, total int, rt string) {
	qrs, total, rt = lst(num)
	return
}
func lst(num model.Num) (qrs []model.Cloth, total int, rt string) {
	db, err := sql.Open("mysql", "root:luyis520@/test?charset=utf8")
	fmt.Println(err)
	rs := []model.Cloth{}
	rows, err := db.Query("SELECT * FROM cloth")
	for rows.Next() {
		var r model.Cloth
		err = rows.Scan(&r.ID, &r.NAME, &r.COLOR)
		//checkErr(err)
		rs = append(rs, r)
	}

	//sort.Sort(sort.Reverse(RecByBM(rs)))
	total = len(rs)
	//qrs = rs[(num.Pageno-1)*num.Pagesize : num.Pageno*num.Pagesize]
	qrs = rs
	rt = "success"
	return
}

// func lst(num model.Num) (qrs []model.Rec, total int, rt string) {
// 	db, err := bolt.Open(model.Cfg.Boltdb, 0666, nil)
// 	if err != nil {
// 		rt = "数据库打开错误！"
// 		return
// 	}
// 	defer db.Close()
// 	rs := []model.Rec{}
// 	err = db.Update(func(tx *bolt.Tx) error {
// 		b, err := tx.CreateBucketIfNotExists([]byte("Rec"))
// 		if err != nil {
// 			return err
// 		}
// 		c := b.Cursor()
// 		for k, v := c.First(); k != nil; k, v = c.Next() {

// 			var r model.Rec
// 			err = json.Unmarshal(v, &r)
// 			if err != nil {
// 				return err
// 			}

// 			rs = append(rs, r)

// 		}
// 		return nil
// 	})
// 	sort.Sort(sort.Reverse(RecByBM(rs)))
// 	total = len(rs)
// 	qrs = rs[(num.Pageno-1)*num.Pagesize : num.Pageno*num.Pagesize]
// 	rt = "success"
// 	return
// }
// func qryrec(bm string) (qrs []model.Rec, rt string) {
// 	db, err := bolt.Open(model.Cfg.Boltdb, 0666, nil)
// 	if err != nil {
// 		rt = "数据库打开错误！"
// 		return
// 	}
// 	defer db.Close()
// 	rs := []model.Rec{}
// 	err = db.Update(func(tx *bolt.Tx) error {
// 		b, err := tx.CreateBucketIfNotExists([]byte("Rec"))
// 		if err != nil {
// 			return err
// 		}
// 		c := b.Cursor()
// 		for k, v := c.First(); k != nil; k, v = c.Next() {

// 			var r model.Rec
// 			err = json.Unmarshal(v, &r)
// 			if err != nil {
// 				return err
// 			}
// 			rs = append(rs, r)

// 		}
// 		return nil
// 	})
// 	//sort.Sort(sort.Reverse(RecByBM(rs)))
// 	var start int
// 	for k, v := range rs {
// 		if v.BM == bm {
// 			start = k
// 		}
// 	}
// 	qrs = rs[:start+30]
// 	rt = "success"
// 	return
// }
// func recbybm(bm string) (rec model.Rec, rt string) {
// 	db, err := bolt.Open(model.Cfg.Boltdb, 0666, nil)
// 	if err != nil {
// 		rt = "数据库打开错误！"
// 		return
// 	}
// 	defer db.Close()
// 	err = db.Update(func(tx *bolt.Tx) error {
// 		b, err := tx.CreateBucketIfNotExists([]byte("Rec"))
// 		if err != nil {
// 			return err
// 		}
// 		c := b.Cursor()
// 		for k, v := c.First(); k != nil; k, v = c.Next() {

// 			var r model.Rec
// 			err = json.Unmarshal(v, &r)
// 			if err != nil {
// 				return err
// 			}
// 			if r.BM == bm {
// 				rec = r
// 				break
// 			}
// 		}
// 		return nil
// 	})
// 	rt = "success"
// 	return
// }
func (rec) Save(cloth model.Cloth) (rt string) {
	rt = save(cloth)
	return
}
func save(cloth model.Cloth) (rt string) {
	db, err := sql.Open("mysql", "root:luyis520@/test?charset=utf8")
	fmt.Println(err)
	fmt.Println(cloth)
	if cloth.ID == 0 {
		stmt, err := db.Prepare("INSERT cloth SET name=?,color=?")
		res, err := stmt.Exec(cloth.NAME, cloth.COLOR)
		fmt.Print(err)
		fmt.Print(res)
	} else {
		fmt.Println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
		stmt, err := db.Prepare("update cloth set name=?,color=? where id=?")
		fmt.Print(err)

		res, err := stmt.Exec(cloth.NAME, cloth.COLOR, cloth.ID)
		fmt.Println(res)
	}

	//sort.Sort(sort.Reverse(RecByBM(rs)))
	rt = "success"
	return

}

func (rec) Del(ids []uint64) (rt string) {
	for _, id := range ids {
		rt = del(id)
		//todo关联删除子表记录,完成
	}
	return
}
func del(id uint64) (rt string) {
	db, err := sql.Open("mysql", "root:luyis520@/test?charset=utf8")
	fmt.Println(err)
	stmt, err := db.Prepare("delete from cloth where id=?")
	//checkErr(err)

	res, err := stmt.Exec(id)
	fmt.Print(res)
	rt = "success"
	return
}

//分割线
//排序函数
